package com.dogsong.liteflow.editor.service.impl;

import com.dogsong.liteflow.editor.service.ExecuteService;
import com.dogsong.liteflow.editor.slot.BaseContext;
import com.dogsong.liteflow.editor.slot.SwitchContext;
import com.dogsong.liteflow.editor.slot.request.SwitchData;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ExecuteServiceImpl implements ExecuteService {
    @Resource
    private FlowExecutor flowExecutor;
    @Override
    public <T, Q extends BaseContext> void execute(String chainId, T requestData, Q... context) {
        LiteflowResponse response;
        if (requestData != null && context != null && context.length > 0) {
            response = flowExecutor.execute2Resp(chainId, requestData, context);
        } else if (requestData != null) {
            response = flowExecutor.execute2Resp(chainId, requestData);
        } else if (context != null && context.length > 0) {
            response = flowExecutor.execute2Resp(chainId, (Object) null, context);
        } else {
            response = flowExecutor.execute2Resp(chainId);
        }

        // 可以在这里处理response结果
        if (!response.isSuccess()) {
            throw new RuntimeException("流程执行失败: " + response.getMessage());
        }
    }
}
